// $Id: ProjectSettings.cs 67 2008-05-07 21:07:39Z nvivo $

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DBLGen.GUI.Forms
{
    public partial class ProjectSettings : Form
    {
        public ProjectSettings(Project project)
        {
            if (project == null)
                throw new ArgumentNullException("project");

            _project = project;

            InitializeComponent();
        }

        private Project _project;

        private PluginInfo _importPlugin;
        private PluginInfo _exportPlugin;
        private PluginSettings _defaultExportSettings;

        private void ProjectSettings_Load(object sender, EventArgs e)
        {
            _importPlugin = _project.ImportPlugin;
            _exportPlugin = _project.ExportPlugin;

            cmbImportPlugins.Items.Add("-- None --");
            cmbImportPlugins.Items.AddRange(Plugin.GetImportPlugins());

            cmbExportPlugins.Items.Add("-- None --");
            cmbExportPlugins.Items.AddRange(Plugin.GetExportPlugins());

            if (_importPlugin != null)
                cmbImportPlugins.SelectedIndex = cmbImportPlugins.FindString(_importPlugin.Name);
            else
                cmbImportPlugins.SelectedIndex = 0;

            if (_exportPlugin != null)
                cmbExportPlugins.SelectedIndex = cmbExportPlugins.FindString(_exportPlugin.Name);
            else
                cmbExportPlugins.SelectedIndex = 0;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            _project.ImportPlugin = _importPlugin;
            _project.ExportPlugin = _exportPlugin;

            if (_exportPlugin != null)
            {
                if (_defaultExportSettings != null)
                    _defaultExportSettings.SaveTo(_project.SettingsManager);
                else
                    _project.SettingsManager.Delete(_exportPlugin.SettingsType, null);
            }

            Close();
        }

        private void cmbPlugins_Format(object sender, ListControlConvertEventArgs e)
        {
            PluginInfo pi = e.ListItem as PluginInfo;

            if (pi != null)
                e.Value = pi.Name;
        }

        private void cmbImportPlugins_SelectedIndexChanged(object sender, EventArgs e)
        {
            _importPlugin = cmbImportPlugins.SelectedItem as PluginInfo;
        }

        private void cmbExportPlugins_SelectedIndexChanged(object sender, EventArgs e)
        {
            _exportPlugin = cmbExportPlugins.SelectedItem as PluginInfo;

            if (cmbExportPlugins.SelectedIndex > 0)
            {
                btnDefaultExportSettings.Enabled = true;
                
                _defaultExportSettings = _exportPlugin.CreateSettings();

                if (_defaultExportSettings != null)
                    _defaultExportSettings.LoadFrom(_project.SettingsManager);
            }
            else
                btnDefaultExportSettings.Enabled = false;
        }

        private void btnDefaultExportSettings_Click(object sender, EventArgs e)
        {
            using (EditPluginSettings frm = new EditPluginSettings(_exportPlugin, _defaultExportSettings))
            {
                DialogResult dr = frm.ShowDialog();
                if (dr == DialogResult.OK)
                    _defaultExportSettings = frm.Settings;
            }
        }
    }
}